<?php
namespace Admin\Controller;
// use Shop\Api\ShopApi;

class ShopController extends AdminController {
   public function index(){
	
		$show=M()->field('a.case,b.title')->table('s_goodshop as a,
    s_cateshop as b')->where('a.case=b.id')->order('a.id desc')->select();
		$title       =   I('name');
        $map['status']  =   array('egt',0);
        if(is_numeric($title)){
            $map['id|name']=   array(intval($name),array('like','%'.$name.'%'),'_multi'=>true);
        }else{
            $map['name']    =   array('like', '%'.(string)$name.'%');
        }

        $list   = $this->lists('goodshop', $map);
      
        foreach($list as $k=>$v){
          $a = $list[$k]['pic'];
          if(strrpos($a, ",")){
            $list[$k]['pic'] =explode(',', $a) ;
          };
        	$list[$k]['gname'] = $show[$k]['title'];
          
        }
        int_to_string($list);
        // var_dump($list);
        // $a =$list['0']['pic'];
        // var_dump(strrpos($a, ","));
        // var_dump(explode(',', $a));
        $this->assign('_list',$list);
        $this->display();
   }


   public function add(){
   	if(IS_POST){
            $upload = new \Think\Upload();// 实例化上传类
		    $upload->maxSize   =     3145728 ;// 设置附件上传大小
		    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    $upload->rootPath  =     './Uploads/Picture/'; // 设置附件上传根目录
		    $info   =   $upload->upload();
			    if(!$info) {// 上传错误提示错误信息
			    	$this->error($upload->getError());
			    }else{// 上传成功
            // var_dump($info);
            foreach ($info as $file) {
              $str .= $file['savepath'].$file['savename'].',';
            }
             $_POST['pic'] = rtrim($str,',');
			    	  // var_dump($_POST['pic']);die;
			    }
			    $shop = D('Shop');
				$res = $shop->create();
				// var_dump($res);die;
				if($res){
					if($shop->add()){
						$this->success('新增成功');
					}else{
						$this->error('新增失败');
					}
				}else{
					$this->error($shop->getError());
	                 }
			}else{
				$cate =M('cateshop')->where(array('pid'=>0))->select();
				$this->assign('cate',$cate);
				$this->meta_title = '新增商品';
				$this->display();
			}	
	}

	public function getchild(){
	    $id = I('post.id');
      // var_dump($id);
	    $cate =M('cateshop')->where(array('pid'=>$id))->select();
      // var_dump($cate);die;
	    if($cate){
	       $this->assign('cate',$cate);
           $res['info'] = $this->fetch('child');
           $res['status'] = 1;
           $res['msg'] = 'success';
	    }else{
           $res['info'] = null;
           $res['status'] = 0;
           $res['msg'] = 'fail';
	    } 
        return $this->ajaxReturn($res);
	}


	public function remove(){
        $cate_id = I('id');
        if(empty($cate_id)){
            $this->error('参数错误!');
        }
        //删除该分类信息
        $res = M('goodshop')->delete($cate_id);
        if($res !== false){
            //记录行为
            action_log('update_goodshop', 'goodshop', $cate_id, UID);
            $re = M('attrkey')->where("gid = $cate_id")->select();
            if(!empty($re)){
               $r =  M('attrkey')->where("gid = $cate_id")->delete();
            }
            $ce = M('attrval')->where("gid = $cate_id")->select();
            if(!empty($ce)){
            $c = M('attrval')->where("gid = $cate_id")->delete();
            }
          $be = M('attrprice')->where("gid = $cate_id")->select();
          if(!empty($be)){
             $b = M('attrprice')->where("gid = $cate_id")->delete();
            }
        
            $this->success('删除商品成功！',$r,$c,$b);
        }else{
            $this->error('删除商品失败！');
        }
    }


    public function edit($id = null){
    	$shop = D('Shop');
    	if(IS_POST){
    		$upload = new \Think\Upload();// 实例化上传类
		    $upload->maxSize   =     3145728 ;// 设置附件上传大小
		    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    $upload->rootPath  =     './Uploads/Picture/'; // 设置附件上传根目录
		    $info   =   $upload->uploadOne($_FILES['pic']);
			    if(!$info) {// 上传错误提示错误信息
			    	$this->error($upload->getError());
			    }else{// 上传成功
			    	$_POST['pic'] = $info['savepath'].$info['savename'];
			    }
			    $res = $shop->create();
          var_dump($res);
			    if($res){
			    	if($shop->save()){
              // var_dump($shop->getLastSql());die;
			    		$this->success('修改商品成功');
			    	}else{
              
			    		$this->error($shop->getLastSql());
			    	}
			    }else{
			    	$error = $shop->getError();
                    $this->error(empty($error) ? '未知错误！' : $error);
			    }

			}else{
				$cate =M('cateshop')->where(array('pid'=>0))->select();
				$this->assign('cate',$cate);

				$info = $id ? $shop->info($id) : '';
				$res = $shop->find(I('get.id'));
				// var_dump($res);die;
				$this->assign('info',   $info);
				$this->assign('res', $res);
				$this->meta_title = '编辑商品';
				$this->display('add');
			}
		}


    public function priceupdate(){
    $id = I('get.id');
    $attr = D('Attrprice');
    if(IS_POST){
      
      
      $res = $attr->create();
      // var_dump($res);die;
      if($res){
        if($attr->save()){
          $this->success('修改价格成功');
        }else{
          $this->error('修改价格失败');
        }
      }else{
        $this->error($attr->getError());
      }
    }else{
      
      $res = M('attrprice')->find("$id");
      $this->assign('res',$res);
      $this->meta_title = '修改价格';
      $this->display();
    }
  }


		public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbidshop':
                $this->xiaja('Shop', $map );
                break;
            case 'resumeshop':
                $this->shangja('Shop', $map );
                break;
            case 'deleteshop':
                $this->del('Shop', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }


    public function priceindex(){
       $name       =   I('name');
        $map['status']  =   array('egt',0);
        if(is_numeric($name)){
            $map['id|name']=   array(intval($name),array('like','%'.$name.'%'),'_multi'=>true);
        }else{
            $map['name']    =   array('like', '%'.(string)$name.'%');
        }

    	  $list   = $this->lists('attrprice', $map);
        int_to_string($list);
        var_dump($list);
        foreach ($list as $val) {

            $good[] = M('goodshop')->field('id,name')->find($val['gid']);
             $where['id'] = array('in',$val['attrvid']); 
             var_dump($where);            
             $where['gid'] = $val['gid'];
            $attr[] = M('attrval')->where($where)->field('id,vname')->select(); 
            // var_dump(M()->getLastSql());
        }

         // var_dump($attr);
         // var_dump($attr,$good,$list);
       foreach ($list as $k => $va) {
          $strval = '';       
       foreach($attr[$k] as $val){          
            $strval .= $val['vname'].'、';

          }
          // var_dump($strval);
        $strval = rtrim($strval,'、');      
        $attrval[] = array('value'=>$strval); 
    } 
    // var_dump($good,$attrval);
      $this->assign('good', $good);
      $this->assign('attrval', $attrval);
      $this->assign('_list', $list);
	    $this->assign('map',$map);
	    $this->meta_title = '价格管理';
	    $this->display(); 
    }

    public function price(){
     
   	if(IS_POST){
   		$mprice = I('post.mprice');
   		$num = I('post.num');
   		$sprice = I('post.sprice');
   		$attrvid = I('post.attrvid');
   		$attr = D('Attrprice');
   	    foreach (I('post.gid') as $k => $v) {
   	    	$data['mprice'] = $mprice[$k];
   	    	$data['sprice'] = $sprice[$k];
   	    	$data['num'] = $num[$k];
   	    	$data['attrvid'] = $attrvid[$k];
   	    	$data['gid'] = $v;
   	   
  	       
   		$res = $attr->create($data);
   		// var_dump($res);die;
   		if($res){
   			if($attr->add()){
   				$this->success('新增价格成功');
   			}else{
   				$this->error('新增价格失败',$attr->getLastSql());
   			}
   		}else{
   			$this->error($attr->getError());
   		}

   	}

   }else{
   	  $id =I('get.id');

   		$sp = M('goodshop')->find($id);
   		$data =  M('attrval');
  	    $re= $data->where("gid = $id")->select();
  	    $sets = array();
  	    $str = '';
  	    foreach ($re as $k=> $v) {
  	    	$sets[$v['kid']][] = $v['vname'];
  	    }
  	      foreach ($re as $k=> $v) {
  	    	$str[$v['kid']][] = $v['id'];
  	    }
  	      
          $arr = $sets = array_values($sets);
          $str = array_values($str);
      
  	      $dd =  $this->dikaer($str);
          $ee = $this->dikaers($arr);
          // var_dump($ee);
        $this->assign('id',$id);
  	    $this->assign('_list',$ee);
        $this->assign('attrvid',$dd);
     		$this->assign('sp',$sp);
     		$this->meta_title = '定制价格';
     		$this->display();
   	}

   }
   function dikaers($arr){  
    $arr1 = array();  
    $result = array_shift($arr);  
    while($arr2 = array_shift($arr)){  
      $arr1 = $result;  
      $result = array();  
      foreach($arr1 as $v){  
        foreach($arr2 as $v2){  
          if(!is_array($v))$v = array($v);  
          if(!is_array($v2))$v2 = array($v2);  
          $result[] = array_merge_recursive($v,$v2);  
        }  
      }  
    }  
    return $result;  
  }  


   public function dikaer($sets){
   	$result = array();
   	for($i=0,$count=count($sets); $i<$count-1; $i++){
   		if($i==0){
   			$result = $sets[$i];
   		}
   		$tmp = array();
   		foreach($result as $res){
   			foreach($sets[$i+1] as $set){
   				$tmp[] = $res.','.$set;
   			}
   		}
   		$result = $tmp;
   	}
   	return $result;
   }




}


	   

?>